package com.tomaer.weibo.rss.reader;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;




public final class WeiboRSSCreator {
	
	static final Log logger = LogFactory.getLog(WeiboRSSCreator.class);
	static final String TENCENT_WEIBO_URL = "http://t.qq.com/";
	static final String SINA_WEIBO_URL1 = "http://weibo.com/";
	static final String SINA_WEIBO_URL2 = "http://v.t.sina.com.cn/widget/widget_blog.php?height=500&skin=wd_01&showpic=1&uid=";
	static final String TYPE_WEIBO_TENCENT = "tencent";
	static final String TYPE_WEIBO_SINA = "sina";
	static final String SINA_WEIBO_CERTIFICATION_YES = "y";
	static final String SINA_WEIBO_CERTIFICATION_NO = "n";
	static final String CHANGE_LINE = "\r\n";
	static final String RSS_XML_HEARER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
	static final String ATOM_XML_HEADER_LINE_1 = "<rss version=\"2.0\"  xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"";
	static final String ATOM_XML_HEADER_LINE_2 = "      xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"";
	static final String ATOM_XML_HEADER_LINE_3 = "      xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\"";
	static final String ATOM_XML_HEADER_LINE_4 = "      xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\">";
	static final String ATOM_NODE_CHANNEL_BEGIN = "<channel>";
	static final String ATOM_NODE_CHANNEL_END = "</channel>";
	static final String ATOM_XML_FOOTER_LINE = "</rss>";
	static final String ATOM_NODE_TITLE_BEGIN = "<title>";
	static final String ATOM_NODE_TITLE_END = "</title>";
	static final String ATOM_NODE_ATOM_LINK_BEGIN = "<atom:link href=\"";
	static final String ATOM_NODE_ATOM_LINK_END = "\" rel=\"self\" type=\"application/rss+xml\" />";
	static final String ATOM_NODE_LINK_BEGIN = "<link>";
	static final String ATOM_NODE_LINK_END = "</link>";
	static final String ATOM_NODE_DESCRIPTION_BEGIN = "<description>";
	static final String ATOM_NODE_DESCRIPTION_END = "</description>";
	static final String ATOM_NODE_LAST_BUILD_DATE_BEGIN = "<lastBuildDate>";
	static final String ATOM_NODE_LAST_BUILD_DATE_END = "</lastBuildDate>";

	static final String RSS_PROVIDER_LINK = "<link >";
	
	public static StringBuilder create(String weiboType,String weiboAccount,String isSinaWeiboCERTIFICATION){
		if((StringUtils.isEmpty(weiboType) || StringUtils.isEmpty(weiboAccount)) || (StringUtils.isEmpty(isSinaWeiboCERTIFICATION) && weiboType.equals(TYPE_WEIBO_SINA))){
			logger.error("Illegal argument exception");
			throw new IllegalArgumentException("Illegal argument exception");
		}
		return createRSS(weiboType,weiboAccount,isSinaWeiboCERTIFICATION);
	}
	
	private static StringBuilder createRSS(String weiboType,String weiboAccount,String isSinaWeiboCERTIFICATION){
		WeiboResolveProvider resolve = null;
		if(weiboType.equals(TYPE_WEIBO_TENCENT)){
			resolve = new TencentWeiboResolve();
		}else if(weiboType.equals(TYPE_WEIBO_SINA)){
			resolve = new SinaWeiboResolve();
		}
		WeiboRSS weiboRSS = null;
		if(weiboType.equals(TYPE_WEIBO_TENCENT)){
			weiboRSS = resolve.resolve(TENCENT_WEIBO_URL+weiboAccount);
		}else if(isSinaWeiboCERTIFICATION.equals(SINA_WEIBO_CERTIFICATION_YES)){
			weiboRSS = resolve.resolve(SINA_WEIBO_URL1+weiboAccount);
		}else if(isSinaWeiboCERTIFICATION.equals(SINA_WEIBO_CERTIFICATION_NO)){
			weiboRSS = resolve.resolve(TENCENT_WEIBO_URL+weiboAccount);
		}
		return convertWeiboRSS2AtomRSS(weiboRSS);
	}
	
	private static StringBuilder convertWeiboRSS2AtomRSS(WeiboRSS weiboRSS){
		StringBuilder rss = new StringBuilder(RSS_XML_HEARER).append(CHANGE_LINE);
		rss.append(ATOM_XML_HEADER_LINE_1).append(CHANGE_LINE);
		rss.append(ATOM_XML_HEADER_LINE_2).append(CHANGE_LINE);
		rss.append(ATOM_XML_HEADER_LINE_3).append(CHANGE_LINE);
		rss.append(ATOM_XML_HEADER_LINE_4).append(CHANGE_LINE);
		rss.append(ATOM_NODE_CHANNEL_BEGIN).append(CHANGE_LINE);
		rss.append(ATOM_NODE_TITLE_BEGIN).append(weiboRSS.getTitle()).append(ATOM_NODE_TITLE_END).append(CHANGE_LINE);
		rss.append(ATOM_NODE_ATOM_LINK_BEGIN).append(weiboRSS.getAtomLink()).append(ATOM_NODE_ATOM_LINK_END).append(CHANGE_LINE);
		rss.append(ATOM_NODE_LINK_BEGIN).append(weiboRSS.getLink()).append(ATOM_NODE_LINK_END).append(CHANGE_LINE);
		rss.append(ATOM_NODE_DESCRIPTION_BEGIN).append(weiboRSS.getDescription()).append(ATOM_NODE_DESCRIPTION_END).append(CHANGE_LINE);
		rss.append(ATOM_NODE_LAST_BUILD_DATE_BEGIN).append(weiboRSS.getLastBuildDate()).append(ATOM_NODE_LAST_BUILD_DATE_END).append(CHANGE_LINE);
		rss.append(ATOM_NODE_CHANNEL_END).append(CHANGE_LINE);
		rss.append(ATOM_XML_FOOTER_LINE);
		return rss;
	}
}
